package com.dream52.pms.sys.controller;

import com.dream52.pms.base.BaseController;
import com.dream52.pms.sys.entity.SysFunction;
import com.dream52.pms.sys.entity.SysFunctionPermissions;
import com.dream52.pms.sys.service.SysFunctionPermissionsService;
import com.dream52.pms.sys.entity.SysUser;
import com.dream52.pms.sys.service.SysFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/sysFunctionPermissions")
public class SysFunctionPermissionsController extends BaseController {
    @Autowired
    private SysFunctionPermissionsService sysFunctionPermissionsService;


    @GetMapping("/list")
    @ResponseBody
    public String getSysFunctionPermissionsList(Integer pageNo, Integer pageSize) throws Exception {
        SysUser user = this.getUserSession();
        String where = "1 = 1";
        if (user != null) {
            if (null == pageNo || pageNo.toString().length() < 1) {
                List sysFunctionPermissionsList = this.sysFunctionPermissionsService.getListByWhere(where, "id ASC");
                this.result.put("sysFunctionPermissionsList", sysFunctionPermissionsList);
            } else {
                PageInfo pageInfo = this.sysFunctionPermissionsService.getPageByWhere(where, pageNo, this.pageSize, "id ASC");
                this.result.put("pageInfo", pageInfo);
            }
            return this.toPageJson(this.SUCCESS_CODE);
        }
        return this.toPageJson(this.ERROR_CODE, "请先登录");
    }

    @GetMapping("/get")
    @ResponseBody
    public String getSysFunctionPermissions(Integer userId) throws Exception {
        SysUser user = this.getUserSession();
        SysFunctionPermissions b = new SysFunctionPermissions();
        if (user != null) {
            if (userId != null && userId > 0) {
                b = this.sysFunctionPermissionsService.getByWhere("userId=" + userId);
            }
            this.result.put("bean", b);
            return this.toPageJson(this.SUCCESS_CODE);
        }
        return this.toPageJson(this.ERROR_CODE, "请先登录");
    }

    @PostMapping("/save")
    @ResponseBody
    public String saveSysFunctionPermissions(SysFunctionPermissions entity) throws Exception {
        SysUser user = this.getUserSession();
        if (entity.getId() == null) {
            this.sysFunctionPermissionsService.save(entity);
        } else {
            this.sysFunctionPermissionsService.update(entity);
        }
        return this.toPageJson(this.SUCCESS_CODE);
    }

    @PostMapping("/del")
    @ResponseBody
    public String delSysFunctionPermissions(Integer id) throws Exception {
        SysUser user = this.getUserSession();
        if (id != null && id.toString().length() > 0) {
            this.sysFunctionPermissionsService.deleteById(id);
        }
        return this.toPageJson(this.SUCCESS_CODE);
    }
}